﻿using ExpressesClass.Model;
using OFFICE_Helper.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace OFFICE_Helper.Forms
{
    public partial class ChoosePrinter : Form
    {
        #region 变量
        List<Kddh> kddhs;
        #endregion

        #region 方法
        public ChoosePrinter(List<Kddh> kddhs)
        {
            InitializeComponent();
            this.kddhs = kddhs;
        }
        #endregion

        #region 事件
        private void ChoosePrinter_Load(object sender, EventArgs e)
        {
            List<string> printers = new List<string>();
            foreach (string p in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                printers.Add(p);
            }
            cb_print.DataSource = printers;

            List<string> kyPrinters = new List<string>();
            kyPrinters.Add(ConfigurationManager.AppSettings["kyPrintClientId"].ToString());
            cb_kyPrint.DataSource = kyPrinters;
        }
        private void btn_ok_Click(object sender, EventArgs e)
        {
            foreach (Kddh k in kddhs)
            {
                if (k.kdCompanys == "跨越快递")
                {
                    KyPrint(k);
                }
                else
                {
                    Print(k);
                }
            }
            this.DialogResult = DialogResult.Yes;
        }
        private void Print(Kddh k)
        {
            DateTime time = DbUtil.GetTime();

            if (!DbUtil.Kddh_Print(k.mailNo))
            {
                MessageBox.Show("面单号:" + k.mailNo + "状态更新失败,请检查网络.", "提示");
            }
            for (int i = k.printBegin; i <= k.printEnd; i++)
            {
                PrintUtil.Print(cb_print.SelectedItem.ToString(), k, i, time);
            }
        }
        private void KyPrint(Kddh k)
        {
            DateTime time = DbUtil.GetTime();
            PrintUtil.Print(cb_kyPrint.SelectedItem.ToString(), k, 1, time);
        }
        #endregion
    }
}
